<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Video extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('videos');
		$this->load->model('courses');
	}

	/**
	** 网课-视频列表
	*/
	public function index($order = 0,$cate = 0,$subject = 0,$limit = 0){
		if($this->input->post()){
			$page=$_POST['page'];
		}
		$offset = 9;
		$info = array('order' => $order , 'cate' => $cate ,'subject' => $subject);
		$course_cate = $this->videos->get_video_cate();
		//$subject_cate = $this->videos->get_course_cate($cate);
		// print_r($course_cate);exit;
		if($limit > 0 && is_ajax_request()) $limit = ($limit - 1) * $offset;
		$list = $this->courses->get_course_list($order,$cate,$subject,$limit,$offset);
		// $this->smarty->assign("data", $data);
        if(is_ajax_request()){
            if(!empty($list['result'])){
                echo json_encode($list['result']);
            }else{
                echo json_encode(array());
            }
            exit;
        }
		$page_config['uri_segment'] = 6;
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/video/index/'.$order.'/'.$cate.'/'.$subject;
		$page_config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("course_cate", $course_cate);
		//$this->smarty->assign("subject", $subject_cate);
		$this->smarty->assign("info", $info);
		$this->smarty->assign("list", $list);
		$this->smarty->assign("menutag", "course");
		$this->smarty->assign("count", ceil($list['count'] / $offset)+1);
		$this->smarty->display("course/video_list.html");

	}


	function get_index_list($order = 0,$cate = 0,$subject = 0,$page = 1){

		$limit = 0;
		$offset = 9;
		if($page > 0){
			$limit = ($page - 1) * $offset;
		}
		$info = array('order' => $order , 'cate' => $cate ,'subject' => $subject);
		//$course_cate = $this->videos->get_video_cate();
		//$subject_cate = $this->videos->get_course_cate($cate);


		$list = $this->courses->get_course_list($order,$cate,$subject,$offset,$limit);
		echo json_encode($list);exit;
	}

	/**
	* 视频课详情
	*/
	public function detail($id = 0){
		$info = $this->videos->get_course_info($id);
		if(empty($info)){
			show_message($_SERVER['HTTP_REFERER'], "该课程不存在！！");exit;
		}
		//var_dump($logged_in);exit;
		$chapter_info = $this->videos->get_video_info($id);
		foreach ($chapter_info as $key => $value) {
			$chapter_info[$this->floatohz($key+1)] = $value;
			unset($chapter_info[$key]);
		}
		/*$order_buy = $this->courses->check_is_buy($id,1);

		if(!empty($order_buy)){
			$effective_time = $order_buy['add_time'] + $order_buy['effective_time']*24*60*60;
			$time = time();
			if($time < $effective_time){
				$is_buy = 1;
			}else{
				$is_buy = 2;
			}
		}else{
			$is_buy = 0;
		}*/
		$is_buy = 0;
		if($this->user_data['id']){
			$crad_info = check_user_learn_card($this->user_data['id'], $id, 1);
			$order_buy = check_effective_time($this->user_data['id'], $id, 1);
			if($crad_info || $order_buy || ($info['is_free'] == 1 && get_vip_endtime($this->user_data['id']) > time())){
				$is_buy = 1;
			}
		}

		$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
		$this->smarty->assign("info", $info);
		$this->smarty->assign("is_buy", $is_buy);
		$this->smarty->assign("chapter", $chapter_info);
		$this->smarty->assign("menutag", "course");
		$this->smarty->display("course/video_detail.html");
	}

	public function video_comment_list($course_id = 0, $limit = 0){


		$offset = 5;
		if($limit > 0 && is_ajax_request()) $limit = ($limit - 1) * $offset;

		$list = $this->videos->get_comment_list($course_id,$limit,$offset);
		$this->smarty->assign("course_id", $course_id);
		if(is_ajax_request()){
			if(!empty($list['result'])){
				foreach ($list['result'] as $key => $value) {
					$list['result'][$key]['add_time'] = date("Y-m-m H:i:s", $value['add_time']);
				}
				echo json_encode($list['result']);
			}else{
				echo json_encode(array());
			}
			exit;
		}
		$page_config['uri_segment'] = 4;
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['total_rows'] = $list['count'];
		$page_config['per_page'] = $offset;
		$page_config['base_url'] = '/video/video_comment_list/'.$course_id;
		$page_config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("count", ceil($list['count'] / $offset)+1);
		$this->smarty->assign("list", $list['result']);
		$this->smarty->display("course/comment_list.html");
	}


  	public function play($id = 0,$chapter_id = 0){
	  	//$value='10';
		//print $this->floatohz($value);exit;
		//echo "<br>";
		$is_buy = 0;
		if($this->user_data['id']){
			$crad_info = check_user_learn_card($this->user_data['id'], $id, 1);
			$order_buy = check_effective_time($this->user_data['id'], $id, 1);
			if($crad_info || $order_buy || get_vip_endtime($this->user_data['id']) > time()){
				$is_buy = 1;
			}
		}
	  	$info = $this->videos->get_course_info($id);
	  	$chapter_info = $this->videos->get_video_info($id);
	  	$count = 0;
	  	foreach ($chapter_info as $key => $value) {
			$chapter_info[$this->floatohz($key+1)] = $value;
			unset($chapter_info[$key]);
		}
		//var_dump($chapter_info);exit;
		foreach ($chapter_info as $key => $value) {
			if(isset($value['jie']) && is_array($value['jie'])){ //用is_array是否为数组
		     	$count += count($value['jie']);
		     	foreach ($value['jie'] as $k => $v) {
		     		$ranking[] = $v;
		     	}
		  	}
		}
		//var
		if($chapter_id == 0){
			$count_ranking = 1;
		}else{
			foreach ($ranking as $key => $value) {
				if($value['id'] == $chapter_id){
					$count_ranking = $key+1;
				}
			}
		}

		//echo $count_ranking;
		//var_dump($ranking);
		//echo $count;
		if($count == 0){
			$count = 1;
		}
		$progress = (int)($count_ranking/$count*100);
		$remaining = $count - $count_ranking;
	  	$video = $this->videos->get_first_video_url($id,$chapter_id);
	  	$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
	  	//print_r($chapter_info);exit;
	  	$this->smarty->assign("chapter", $chapter_info);
	  	$this->smarty->assign("menutag", "course");
	  	$this->smarty->assign("video", $video);
	  	$this->smarty->assign("progress", $progress);
	  	$this->smarty->assign("remaining", $remaining);
	  	$this->smarty->assign("info", $info);
	    $this->smarty->display("course/video_play.html");
	}


  	function floatohz($value){
	    $result='';
		$v_a=array('','','','','十',',百','千','万','十','百','千','亿');
	    $v_b=array('零','一','二','三','四','五','六','七','八','九','十');
	    $v_c=array();
	    $value=(string)$value;
	    $value=sprintf("%0.2f",$value);
	    $len=strlen($value);
	    for($i=$len;$i>=0;$i--){
	       $val = @$value[$i]; //$VALUE 不是数组
	       if($val!='.'){
	           if($val!='0')
	              @$v_c[]=$v_b[$val].$v_a[$len-$i-1];
	       }
	    }
	    $v_c=array_reverse($v_c);
	    foreach($v_c as $val){
	       $result.=$val;
	    }
	    unset($v_a);unset($v_b);unset($v_c);
	    return $result;
	}


}
